"use strict";var PortHandler=new function(){this.main_timeout_reference,this.initial_ports=!1,this.port_detected_callbacks=[],this.port_removed_callbacks=[]};PortHandler.initialize=function(){this.check()},PortHandler.check=function(){function e(){chrome.usb.getDevices(usbDevices.STM32DFU,function(e){e.length?$("div#port-picker #port [value='DFU']").length||($("div#port-picker #port").append('<option value="DFU">DFU</option>'),$("div#port-picker #port").val("DFU")):$("div#port-picker #port [value='DFU']").length&&$("div#port-picker #port [value='DFU']").remove()})}var t=this;serial.getDevices(function(r){if(t.array_difference(t.initial_ports,r).length>0||!t.initial_ports){var o=t.array_difference(t.initial_ports,r);if(0!=t.initial_ports&&(o.length>1?console.log("PortHandler - Removed: "+o):console.log("PortHandler - Removed: "+o[0])),delete CONFIG.boardVersion,delete CONFIG.firmwareVersion,setFirmwareLabels(),GUI.connected_to)for(var i=0;i<o.length;i++)o[i]==GUI.connected_to&&$("div#port-picker a.connect").click();if(t.update_port_select(r),t.initial_ports)for(var i=t.port_removed_callbacks.length-1;i>=0;i--){var l=t.port_removed_callbacks[i];clearTimeout(l.timer),l.code(o);var a=t.port_removed_callbacks.indexOf(l);a>-1&&t.port_removed_callbacks.splice(a,1)}if(t.initial_ports||chrome.storage.local.get("last_used_port",function(e){e.last_used_port?r.forEach(function(t){t==e.last_used_port&&(console.log("Selecting last used port: "+e.last_used_port),$("div#port-picker #port").val(e.last_used_port))}):console.log('Last used port wasn\'t saved "yet", auto-select disabled.')}),t.initial_ports)for(var i=0;i<o.length;i++)t.initial_ports.splice(t.initial_ports.indexOf(o[i]),1);else t.initial_ports=r}var c=t.array_difference(r,t.initial_ports);if(c.length){c.length>1?console.log("PortHandler - Found: "+c):console.log("PortHandler - Found: "+c[0]),t.update_port_select(r),GUI.connected_to?$("div#port-picker #port").val(GUI.connected_to):($("div#port-picker #port").val(c[0]),GUI.wrong_port=!1,refreshConnectButton());for(var i=t.port_detected_callbacks.length-1;i>=0;i--){var l=t.port_detected_callbacks[i];clearTimeout(l.timer),l.code(c);var a=t.port_detected_callbacks.indexOf(l);a>-1&&t.port_detected_callbacks.splice(a,1)}t.initial_ports=r}GUI.optional_usb_permissions&&e(),t.main_timeout_reference=setTimeout(function(){t.check()},250)})},PortHandler.update_port_select=function(e){if($("div#port-picker #port").html(""),e.length>0)for(var t=0;t<e.length;t++)$("div#port-picker #port").append($("<option/>",{value:e[t],text:e[t]}));else $("div#port-picker #port").append($("<option/>",{value:0,text:"No Ports"}))},PortHandler.port_detected=function(e,t,r,o){var i=this,l={name:e,code:t,timeout:r?r:1e4};return o?(l.timer=!1,l.timeout=!1):l.timer=setTimeout(function(){console.log("PortHandler - timeout - "+l.name),t(!1);var e=i.port_detected_callbacks.indexOf(l);e>-1&&i.port_detected_callbacks.splice(e,1)},r?r:1e4),this.port_detected_callbacks.push(l),l},PortHandler.port_removed=function(e,t,r,o){var i=this,l={name:e,code:t,timeout:r?r:1e4};return o?(l.timer=!1,l.timeout=!1):l.timer=setTimeout(function(){console.log("PortHandler - timeout - "+l.name),t(!1);var e=i.port_removed_callbacks.indexOf(l);e>-1&&i.port_removed_callbacks.splice(e,1)},r?r:1e4),this.port_removed_callbacks.push(l),l},PortHandler.array_difference=function(e,t){for(var r=[],o=0;o<e.length;o++)r.push(e[o]);for(var o=0;o<t.length;o++)r.indexOf(t[o])!=-1&&r.splice(r.indexOf(t[o]),1);return r},PortHandler.flush_callbacks=function(){for(var e=0,t=this.port_detected_callbacks.length-1;t>=0;t--)this.port_detected_callbacks[t].timer&&clearTimeout(this.port_detected_callbacks[t].timer),this.port_detected_callbacks.splice(t,1),e++;for(var t=this.port_removed_callbacks.length-1;t>=0;t--)this.port_removed_callbacks[t].timer&&clearTimeout(this.port_removed_callbacks[t].timer),this.port_removed_callbacks.splice(t,1),e++;return e};